<?php

use console\db\channels\TransferTableMigration;

/**
 * 圈子帖子表迁移
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m300020_000003_t_sns_dynamic extends TransferTableMigration {

    /**
     * @inheritdoc
     */
    public $sourceDb = 'olddb';

    /**
     * @inheritdoc
     */
    public function validateTransfer(&$data) {
        if (parent::validateTransfer($data)) {
            if ($data['gid'] == 1) {
                return true;
            }
        }
        return false;
    }

    /**
     * @inheritdoc
     */
    public function getTransferOptions() {
        return [
            'table' => [
                'sns_dynamic' => 'sns_group_thread',
            ],
            'columns' => [
                'id' => 'tid',
                'user_id' => 'uid',
                'group_id' => 'gid',
                'subject' => 'title',
                'best_at' => function($data) {
                    if ($data['digest']) {
                        return $data['inputtime'] + rand(3600, 86400);
                    }
                    return 0;
                },
                'recommend_at' => function($data) {
                    if ($data['topped']) {
                        return $data['inputtime'] + rand(3600, 86400);
                    }
                    return 0;
                },
                'status' => function($data) {
                    if ($data['status'] > 0) {
                        return 10;
                    }
                    return $data['status'];
                },
                'created_at' => 'inputtime',
                'updated_at' => function($data) {
                    if ($data['lastposttime']) {
                        return $data['lastposttime'];
                    }
                    return $data['inputtime'] + rand(10, 1800);
                },
                'deleted_at' => function($data) {
                    if ($data['status'] < 0) {
                        if ($data['lastposttime']) {
                            return $data['lastposttime'];
                        }
                        return $data['inputtime'] + rand(10, 1800);
                    }
                    return 0;
                },
            ],
        ];
    }

}
